<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Common;
use app\admin\service\VideoService;
use app\common\StsService;
use app\admin\service\VideoDetailService;

class VideoDetail extends Base
{
    private $service;
    private $videoDetailService;

    public function __construct(VideoService $service, VideoDetailService $videoDetailService)
    {
        parent::__construct();
        $this->service = $service;
        $this->videoDetailService = $videoDetailService;
    }

    /**
     * 渲染视频列表页面
     */
    public function index()
    {
        return view();
    }

    /**
     * 渲染视频详情页面
     */
    public function info()
    {
        return view();
    }

    // 视频列表
    public function list()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getVideoDetailList($params);
        return Common::success($data);
    }

    /**
     * 视频详情
     */
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getDetailsOfEachEpisode($params['id']);
        return Common::success($data);
    }

    /**
     * 视频直传凭证
     * 
     * @date 2022/08/11
     * @return json
     */
    public function uploadCredentials()
    {
        $object = new StsService();
        $data = $object->getStsAuth();

        if ($data['statusCode'] != 200) {
            return Common::fail($data['errorMessage']);
        }

        return Common::success($data);
    }

    // 添加剧集
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editVideoDetail($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 修改剧集
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editVideoDetail($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 删除视频
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteVideoDetail($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 渲染设置收费页面
     */
    public function charge()
    {
        return view();
    }

    public function mount()
    {
        return view();
    }

    public function mount_info()
    {
        return view();
    }
    

    // 设置从第多少集后开始收费
    public function setCharge()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('set_charge')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->setCharge($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 取消收费
    public function cancelCharge()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('cancel_charge')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->cancelCharge($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 批量上传
    public function batchUpload()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('batch_upload')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->batchAddVideoDetail($params);


        if(!empty($result['msg'])){
            return Common::fail($result['msg']);
        }

        return Common::success();
    }

    // 批量删除
    public function batchDelete()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\VideoDetail::class)->scene('batch_delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->batchDeleteVideoDetail($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 挂载的小程序自定义收费配置
    public function rechargeConfig()
    {
        $params = Request::param();
        $result = $this->videoDetailService->editMountedRechargeConfig($params);

        if (!empty($result['message'])) {
            return Common::fail($result['message']);
        }

        return Common::success();
    }

    // 修改挂载的小程序自定义收费配置
    public function editRechargeConfig()
    {
        $params = Request::param();
        $result = $this->videoDetailService->editMountedRechargeConfig($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 根据短剧ID获取配置的自定义收费配置
    public function getRechargeConfig()
    {
        $params = Request::param();
        $data = $this->videoDetailService->getMountedRechargeConfigByVideoId($params['video_id'],$params['detail_id']);
        return Common::success($data);
    }

    // 根据ID获取配置的自定义收费配置
    public function rechargeConfigDetail()
    {
        $params = Request::param();
        $data = $this->videoDetailService->getMountedRechargeConfigDetailById($params['id']);

        return Common::success($data);
    }
}